package com.attendance.service.impl;

import com.attendance.common.CommonResult;
import com.attendance.mapper.AdminMapper;
import com.attendance.mapper.StudentMapper;
import com.attendance.mapper.TeacherMapper;
import com.attendance.pojo.Admin;
import com.attendance.pojo.Student;
import com.attendance.pojo.Teacher;
import com.attendance.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @see com.attendance.service.UserService 实现类
 *
 * */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private StudentMapper studentMapper;

    //管理员登录service
    public CommonResult adminLogin(String name, String password) {
        Admin admin = adminMapper.selectByAccountAndPW(name, password);
        //验证管理员账号密码是否匹配
        if (admin!=null){
            return CommonResult.success(admin,"登录成功");
        }else {
            return CommonResult.failed("账号或密码错误");
        }
    }

    //教师登录service
    public CommonResult teacherLogin(String name, String password) {
        Teacher teacher = teacherMapper.selectByWorkNumberAndPW(name, password);
        if (teacher!=null){
            return CommonResult.success(teacher,"登录成功");
        }else {
            return CommonResult.failed("账号或密码错误");
        }
    }

    //学生登录service
    public CommonResult studentLogin(String name, String password) {
        Student student = studentMapper.selectByStuNumAndPW(name, password);
        if (student!=null){
            return CommonResult.success(student,"登录成功");
        }else {
            return CommonResult.failed("账号或密码错误");
        }

    }

    //管理员修改密码
    @Override
    public CommonResult adminUpdatePw(String name, String password) {
        int i = adminMapper.updateByAccountAndPW(name, password);
        if (i==1){
            return CommonResult.success(null,"修改成功");
        }else{
            return CommonResult.failed("密码修改失败");
        }
    }

    //教师修改密码
    @Override
    public CommonResult teacherUpdatePw(String name, String password) {
        int i = teacherMapper.updateByWorkNumberAndPW(name, password);
        if (i==1){
            return CommonResult.success(null,"修改成功");
        }else{
            return CommonResult.failed("密码修改失败");
        }
    }

    //学生修改密码
    @Override
    public CommonResult studentUpdatePw(String name, String password) {
        int i = studentMapper.updateByStuNumAndPW(name, password);
        if (i==1){
            return CommonResult.success(null,"修改成功");
        }else{
            return CommonResult.failed("密码修改失败");
        }
    }

}
